package com.feeyin.radar.msg.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.dao.CommentDAO;
import com.feeyin.radar.msg.dao.MessageDAO;
import com.feeyin.radar.msg.model.Comment;
import com.feeyin.radar.msg.model.Message;
import com.feeyin.radar.msg.service.EmployeeBBSService;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class EmployeeBBSServiceImpl
  implements EmployeeBBSService
{
  private MessageDAO messageDAO;
  private CommentDAO commentDAO;
  private UserDAO userDAO;

  public MessageDAO getMessageDAO()
  {
    return this.messageDAO;
  }

  public void setMessageDAO(MessageDAO messageDAO) {
    this.messageDAO = messageDAO;
  }

  public CommentDAO getCommentDAO() {
    return this.commentDAO;
  }

  public void setCommentDAO(CommentDAO commentDAO) {
    this.commentDAO = commentDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public List getPageEmployeeBBSs(Page page, String subject) {
    if (page == null) return null;

    List employeeBBSs = new ArrayList();
    Message message = new Message();
    message.setMessageType("2");
    if (!StringUtils.isEmpty(subject)) {
      message.setSubject(subject);
    }
    employeeBBSs = this.messageDAO.getPageMessagesByDynamic(message, page);

    if (employeeBBSs == null) return null;
    for (int i = 0; i < employeeBBSs.size(); i++) {
      message = new Message();
      message = (Message)employeeBBSs.get(i);
      if (message.getSendPersonId() != null) {
        Users user = new Users();
        user = this.userDAO.getUser(message.getSendPersonId());
        if (user != null) {
          message.setSendPerson(user.getUserName());
        }
      }
      message.getComments().addAll(this.commentDAO.getCommentsBymessageId(message.getId()));
    }
    return employeeBBSs;
  }

  public Integer getPageEmployeeBBSsCount(String subject) {
    Integer count = Integer.valueOf(0);
    Message message = new Message();
    message.setMessageType("2");
    if (!StringUtils.isEmpty(subject)) {
      message.setSubject(subject);
    }
    count = this.messageDAO.getPageMessagesCountByDynamic(message);
    return count;
  }

  public Message getEmployeeBBSById(Integer id)
  {
    if (id == null) return null;
    Message message = new Message();
    message = this.messageDAO.getMessageById(id);

    if (message == null) return null;
    Users user = new Users();
    user = this.userDAO.getUser(message.getSendPersonId());
    if (user != null) {
      message.setSendPerson(user.getUserName());
    }

    List comments = new ArrayList();
    comments = this.commentDAO.getCommentsBymessageId(message.getId());
    if ((comments != null) && (comments.size() > 0)) {
      for (int i = 0; i < comments.size(); i++) {
        Comment comment = new Comment();
        comment = (Comment)comments.get(i);
        if (comment.getCommentPersonId() != null) {
          user = new Users();
          user = this.userDAO.getUser(comment.getCommentPersonId());
          if (user != null) {
            comment.setCommentPerson(user.getUserName());
          }
        }
      }
      message.getComments().addAll(comments);
    }

    return message;
  }

  public boolean insertEmployeeBBS(Message message) {
    if (message == null) return false;
    String messageCode = DateUtils.getDateByFormat("yyyyMMddHHmmss");
    message.setMessageCode(messageCode);
    message.setMessageType("2");
    this.messageDAO.insertMessage(message);
    message = new Message();
    message = this.messageDAO.getMessageByMessageCode(messageCode);
    return message != null;
  }

  public boolean insertEmployeeBBSComment(Comment comment)
  {
    if (comment == null) return false;
    String commentCode = DateUtils.getDateByFormat("yyyyMMddHHmmss");
    comment.setCommentCode(commentCode);
    comment.setCommentDate(new Date());
    return this.commentDAO.insertComment(comment);
  }

  public boolean updateEmployeeBBSViewNum(Integer id, Integer viewNum) {
    return this.messageDAO.updateMessageViewNum(id, viewNum);
  }

  public boolean deleteEmployeeBBS(String[] ids) {
    if ((ids == null) || (ids.length <= 0)) return false;

    for (int i = 0; i < ids.length; i++) {
      if (!StringUtils.isEmpty(ids[i])) {
        boolean result = this.messageDAO.deleteMessage(Integer.valueOf(Integer.parseInt(ids[i])));
        if (result) {
          this.commentDAO.deleteCommentByMessageId(Integer.valueOf(Integer.parseInt(ids[i])));
        }
      }
    }
    return true;
  }

  public boolean deleteEmployeeBBSComment(String[] ids) {
    if ((ids == null) || (ids.length <= 0)) return false;

    for (int i = 0; i < ids.length; i++) {
      if (!StringUtils.isEmpty(ids[i])) {
        this.commentDAO.deleteCommentById(Integer.valueOf(Integer.parseInt(ids[i])));
      }
    }
    return true;
  }
}